// pages/me/userInfo/userInfo.js
const app = getApp()
import { memverUser, getProv, memberUserinfo } from '../../../api/api.js';
import { tips } from '../../../utils/show.js';
import { formatDate } from '../../../utils/util.js';


Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: app.globalData.height * 2 + 30,
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '完善个人信息  ', //导航栏 中间的标题
      img: "../../image/back.png"
    },
    realname:'',
    sex: 1,
    birthday: '请选择生日',
    idcard:'',
    email:'',
    address: '',
    img1:"/image/xl2.png",
    data: {},
    provinceShow: false,    // 省份控制下拉列表的显示隐藏，false隐藏、true显示
    provinceSelectData: [], // 省份下拉列表的数据
    provText: { id: 0, val: '请选择省份' }, 
    cityShow: false,                          // 城市控制下拉列表的显示隐藏，false隐藏、true显示
    cityText: { provincecode: 0, citycode:0, val: '请选择城市'},      // 城市下拉列表的数据
    cityArr: [],
    cityIndex: 0                              // 城市选择的下拉列表下标
  },

  onLoad: function (options) {
    this.getUserList();
    this.getProvince(); //获取省份
  },
  getUserList(){
    let data = {
      openid: app.globalData.openid,
    }
    app.showLoading();
    memverUser(data).then(res =>{
      app.hideLoading();
      if (res.code == 0) {
        // console.log(res);
        let userInfo = res.data;
        if (res.data == null) return;
        let { realname, sex, birthday, idcard, email, address, province, city} = userInfo;
        if (sex == 0) {
          sex = 1;
        }
        if (birthday == 0){
          birthday = '请选择生日';
        } else {
          let time = new Date(birthday * 1000);
          let y = time.getFullYear();
          let mouth = time.getMonth() + 1;
          let m = mouth < 10 ? '0' + mouth : mouth;
          let d = time.getDate();
          birthday = y + '-' + m + '-' + d;
        }
        if (!province || province == 0) {
          province = '请选择省份';
        }
        if (!city) {
          city = '请选择城市';
        }
        this.setData({
          realname, sex, birthday, idcard, email, address,
          'provText.val': province,
          'cityText.val': city
        });
      }else{
        tips(res.msg);
      }
    })
  },

  userName(e){
    let realname = e.detail.value;
     this.setData({ realname });
  },

  setSex(e){
    let v = e.currentTarget.dataset['index'];
    if (v == 2){
      this.setData({ sex: 2 });
    } else {
      this.setData({ sex: 1 });
    }
  },

  bindDateChange (e) {
    this.setData({ birthday: e.detail.value });
  },

/**
 * 获取身份证
 */
  getCard(e){
    let idcard = e.detail.value;
    this.setData({ idcard });
  },

  getEmail(e) {
    let email = e.detail.value;
    this.setData({ email });
  },  

  getAddress(e) {
    let address = e.detail.value;
    this.setData({ address });
  },  
/**
 * 保存
 */
  preservatione(){
    const { realname, sex, birthday, idcard, email, address} = this.data;
    const province = this.data.provText.val;
    const city = this.data.cityText.val;
    let el=/^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
    if (!realname) {
      tips('姓名为空！');
      return;
    }
    if (sex != 1 && sex != 2) {
      tips('请选择性别!')
      return;
    }
    if (birthday === '请选择生日') {
      tips('请选择生日');
      return;
    }
    if(idcard.length != 18 && idcard.length != 15){
      tips("身份证格式不对");
      return
    }
    if (!el.test(this.data.email)){
      tips("邮箱格式不对");
      return
    }
    if (province === '请选择省份' ) {
      tips("请选择省份");
      return
    }
    if (city === '请选择城市' ) {
      tips("请选择城市");
      return
    }
    if (!address) {
      tips("请填写地址");
      return
    }
    const openid = app.globalData.openid;
    if (!openid) {
      tips("请退出重进");
      return
    }
    const data = {
      openid, realname, sex, birthday, idcard, email, province, city, address
    }
    memberUserinfo(data).then(res => {
      if (res.code === 0) {
        app.showToast('已更新个人资料');
        app.globalData.userInfo.total_point = res.data.total_point;
        app.globalData.userInfo.used_point = res.data.used_point;
        wx.navigateBack();
      } else {
        app.showToast('更新资料失败');
      }
    });
  },
  /**
   * 公用if判断
   */
  publicInspect(condition,msg){
    if (condition) {
      tips(msg);
      return true;
    }
    return false;
  },

  /**省份选择点击下拉显示框 **/
  provinceTap() {
    this.setData({
      provinceShow: !this.data.provinceShow
    });
  },

  cityTap () {
    if (this.data.cityArr.length > 0) {
      this.setData({
        cityShow: !this.data.cityShow
      });
    }
  },
  
   /**点击下拉列表 **/
  provinceOptionTap(e) {
    const index = e.currentTarget.dataset.index;
    const data = this.data.data;
    this.setData({
      provinceShow: !this.data.provinceShow,
      cityShow: false,
      city: data[index].cities,
      'provText.id': index,
      'provText.val': data[index].provincename_cn,
      cityArr: data[index].cities,
      cityText: { provincecode: 0, citycode:0, val: '请选择城市'}
    });
  },

  cityeOptionTap (e) {
    const obj = e.currentTarget.dataset;
    // console.log(obj);
    this.setData({
      cityShow: false,
      'cityText.val': obj.val,
      'cityText.provincecode': obj.provcode,
      'cityText.citycode': obj.citycode,
    })
    // console.log(this.data.cityText);
  },

  /**获取*/
  getProvince(){
    getProv().then(res =>{
      // console.log(res)
      // console.log(res.data);
      const data = res.data;
      const prov = []; // 存放省份
      for (let i in data) {
        prov.push({ id: data[i].provincecode, val: data[i].provincename_cn });
      }
      this.setData({
        provinceSelectData: prov,
        data
      })
    })
  }
})